package com.alumni_circle.core.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 龙斌
 * @since 2020-07-03 11:38
 *
 * 赋值给另一对象的相同属性值
 */
public class ModelUtils {

    /**
     * 实体转换
     * @param source 源对象
     * @param targetClass 目标对象的Class类型
     * @return
     */
    public static <S, T> T transform(S source, Class<? extends T> targetClass) {
        if (!ObjectUtils.isNull(source)) {
            T target = null;
            try {
                target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target);
            } catch (Exception e) {
                return null;
            }
            return target;
        } else {
            return null;
        }
    }

    public static  <S,T> List<T>copyListProperties(List<S>sourceList , Class<? extends T> targetClass){
        List<T> result = new ArrayList<T>();
        if (!ObjectUtils.isNull(sourceList)) {
            for (S source : sourceList) {
                result.add(transform(source, targetClass));
            }
        }
        return result;
    }

}
